package httptools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import sslSocketFactory.CustomerSocketFactory;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpTools {
	private  Context context;
	private ProgressDialog dialog;

	public HttpTools(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		dialog = new ProgressDialog(context);
		dialog.setTitle("提示");
		dialog.setMessage("正在连接12306");
	}

	
	
	public void sendDataThread(final String urlPath,
			final Map<String, Object> params, final DownLoadCallBack callback) {

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				String resultCode = (String) msg.obj;
				// System.out.print(resultCode);
				callback.loadContent(resultCode);
				if (msg.what == 1) {
					dialog.dismiss();
				}
				if (msg.what == 0) {
					dialog.dismiss();
					// Toast.makeText(context,
					// "��������ʧ�ܣ��������磡",Toast.LENGTH_LONG).show();
				}
			}
		};

		class MyThread implements Runnable {
			

			@Override
			public void run() {
				// TODO Auto-generated method stub
				//CustomerSocketFactory customerSocketFactory=new CustomerSocketFactory(truststore)
				HttpClient httpClient = null;
				//= new HttpClient();
				httpClient=CustomerSocketFactory.getHttpClient(context);
				HttpPost httpPost = new HttpPost(urlPath);
				String result = "";
				List<BasicNameValuePair> paramete = new ArrayList<BasicNameValuePair>();
				try {
					if (params != null && !params.isEmpty()) {
						for (Map.Entry<String, Object> entry : params
								.entrySet()) {
							String username = entry.getKey();
							String value = entry.getValue().toString();
							BasicNameValuePair valuePair = new BasicNameValuePair(
									username, value);
							paramete.add(valuePair);
						}
					}
					// ���ı������������ļ�
					UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(
							paramete, "utf-8");
					httpPost.setEntity(encodedFormEntity);
					HttpResponse httpResponse = httpClient.execute(httpPost);
					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						
						Log.v("连接成功", "连接成功");
						Log.v("", "");
						result = EntityUtils.toString(httpResponse.getEntity(),
								"utf-8");
						Log.v("获得的json", result);
						String Code = new String();
						Code = result;
						Message message = Message.obtain();
						message.obj = Code;
						message.what = 1;
						handler.sendMessage(message);
					} else {
						Message message = Message.obtain();
						message.obj = null;
						message.what = 0;
						handler.sendMessage(message);

					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					httpClient.getConnectionManager().shutdown();
				}
			}
		}

		new Thread(new MyThread()).start();
		dialog.show();
	}

	public interface DownLoadCallBack {
		public void loadContent(String result);
	}

	/**
	 * �ύ���ݷ���
	 * 
	 * @param urlPath
	 *            ������·��
	 * @param map
	 *            �ϴ���
	 * @param encode
	 * @return
	 */
	public static String sendPostMethod(String urlPath,
			Map<String, Object> params, String encode, Context context) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(urlPath);
		String result = "";
		List<BasicNameValuePair> paramete = new ArrayList<BasicNameValuePair>();
		try {
			if (params != null && !params.isEmpty()) {
				for (Map.Entry<String, Object> entry : params.entrySet()) {
					String username = entry.getKey();
					String value = entry.getValue().toString();
					BasicNameValuePair valuePair = new BasicNameValuePair(
							username, value);
					paramete.add(valuePair);
				}
			}
			// ���ı������������ļ�
			UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(
					paramete, encode);
			httpPost.setEntity(encodedFormEntity);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(httpResponse.getEntity(), encode);
			} else {

				// System.out.print("232522");
				// Toast.makeText(context, "�������������������",
				// Toast.LENGTH_LONG).show();
				result = "false";

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
